﻿using AlibabaCloud.SDK.VIAPI.Utils;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.imageaudit.Model.V20191230;
using aliyun_net_sdk_core_extension.Enum;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace aliyun_net_sdk_core_extension
{
    /// <summary>
    /// https://help.aliyun.com/document_detail/151941.html?spm=a2c4g.11186623.6.678.243312f9OvotQ8
    /// </summary>
    public class AliyunAcsService : IAliyunAcsService
    {
        private IAcsClientService _acsClientService;

        public AliyunAcsService(IAcsClientService acsClientService)
        {
            _acsClientService = acsClientService;
        }

        public string FileUpload(string localImageUrl)
        {
            var config = _acsClientService.AcsConfig;
            var fileUtils = FileUtils.getInstance(config.AppKey, config.AppSecret);

            //转换成上海oss image url
            var ossurl = fileUtils.Upload(localImageUrl);
            return ossurl;
        }

        public string FileUpload(string fileName, byte[] buffer)
        {
            var config = _acsClientService.AcsConfig;
            var fileUtils = FileUtils.getInstance(config.AppKey, config.AppSecret);

            //转换成上海oss image url
            var ossurl = fileUtils.Upload(fileName, new MemoryStream(buffer));
            return ossurl;
        }

        public ScanTextResponse ScanText(string text)
        {
            var request = new ScanTextRequest();
            request.Taskss = new List<ScanTextRequest.Tasks>()
            {
                new ScanTextRequest.Tasks(){ Content = text }
            };
            request.Labelss = new List<ScanTextRequest.Labels>()
            {
                //new ScanTextRequest.Labels(){ Label = ScanLabel.porn.ToString() },
                new ScanTextRequest.Labels(){ Label = ScanLabel.abuse.ToString() }
            };

            ScanTextResponse data = _acsClientService.GetClient().GetAcsResponse(request);
            return data;
        }

        public ScanImageResponse ScanImage(string[] imageUrls)
        {
            var request = new ScanImageRequest();
            request.Scenes = new List<string>()
            {
                ScanSence.porn.ToString()
            };
            request.Tasks = imageUrls.Select(imageUrl => new ScanImageRequest.Task()
            {
                ImageURL = imageUrl,
                DataId = Guid.NewGuid().ToString(),

            }).ToList();

            ScanImageResponse data = _acsClientService.GetClient().GetAcsResponse(request);
            return data;
        }

    }
}
